from django.views.decorators.csrf import csrf_exempt
from rest_framework.decorators import api_view
from rest_framework.response import Response
from Util.Return import get_next_available_name
from Util.SQL import *


@csrf_exempt
@api_view(['GET', 'POST'])
def get_dataset(request):
    sql_string = "SELECT * FROM dataset;"
    data = select(sql_string)
    sentinel = []
    landsat = []
    terrascope = []
    for item in data:
        if item["type"] == "Sentinel":
            sentinel.append(item)
        elif item["type"] == "Landsat":
            landsat.append(item)
        elif item["type"] == "Terrascope":
            terrascope.append(item)
    data_dict = {"sentinel": sentinel, "landsat": landsat, "terrascope": terrascope}
    return Response(data=data_dict)


@csrf_exempt
@api_view(['GET', 'POST'])
def change_dataset(request):
    data_dict = request.data
    update_data = {"name": data_dict['name'], "type": data_dict['type'],
                   "attribute": data_dict['attribute'], "available": data_dict['available']}
    condition = f"dataset_id = '{data_dict['datasetId']}'"
    status = update(update_data, condition, "dataset")
    return Response(data=status)
